﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sac.Data;
using System.Configuration;
using System.Data;

namespace Sac.Web
{
    public class RolePowerOperate
    {
        OledbPro oledb;

        public RolePowerOperate()
        {
            string ConnStr = ConfigurationManager.AppSettings["access"].ToString();
            oledb = new OledbPro(ConnStr);
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="roleName">新增的角色名</param>
        /// <returns>操作是否成功</returns>
        public bool InsertRole(string roleName)
        {
            string sqlInsert = "insert into ROLE_POWER(role_name) values ('" + roleName + "')";
            int i = oledb.ExecuteNonQuery(sqlInsert);
            if (i == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="role">包含角色信息的对象</param>
        /// <returns>操作是否成功</returns>
        public bool DeleteRoleById(string roleId)
        {
            UserRoleOperate user = new UserRoleOperate();
            if (user.GetUserCount(roleId) == 0)
            {
                string sqlDelete = "delete from role_power where id_key=" + roleId;
                int i = oledb.ExecuteNonQuery(sqlDelete);
                if (i == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改角色名
        /// </summary>
        /// <param name="idKey">角色ID号</param>
        /// <param name="roleName">新的角色名</param>
        /// <returns>操作是否成功</returns>
        public bool UpdateRole(string idKey, string roleName)
        {
            string sqlUpdate = "update ROLE_POWER set role_name='" + roleName + "'" + " where id_key=" + idKey;
            int i = oledb.ExecuteNonQuery(sqlUpdate);
            if (i == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="roleName">角色名</param>
        /// <param name="powerCode">权限码</param>
        /// <returns>操作是否成功</returns>
        public bool UpdateRolePowerByName(string roleName, string powerCode)
        {
            string sqlUpdatePower = "update ROLE_POWER set POWER_CODE='" + powerCode + "' where role_name='" + roleName + "'";
            int i = oledb.ExecuteNonQuery(sqlUpdatePower);
            if (i == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="idKey">角色ID</param>
        /// <param name="powerCode">权限码</param>
        /// <returns>操作是否成功</returns>
        public bool UpdateRolePowerById(string idKey, string powerCode)
        {
            string sqlUpdatePower = "update ROLE_POWER set POWER_CODE='" + powerCode + "' where id_key=" + idKey;
            int i = oledb.ExecuteNonQuery(sqlUpdatePower);
            if (i == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取所有角色信息（不包括权限码）
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllRole()
        {
            string sqlSelectAllRole = "select id_key,role_name from role_power order by id_key";
            DataTable dtAllRole = oledb.GetDataTable(sqlSelectAllRole);
            return dtAllRole;
        }

        /// <summary>
        /// 根据角色号获取权限码
        /// </summary>
        /// <param name="idKey">角色代码</param>
        /// <returns>权限码</returns>
        public string GetPowerCodeById(string idKey)
        {
            string sqlSelectPowerId = "select power_code from role_power where id_key=" + idKey;
            string powerCode = "";
            try
            {
                powerCode = oledb.GetDataRow(sqlSelectPowerId)[0].ToString();
            }
            catch
            {
            }
            return powerCode;
        }

        /// <summary>
        /// 根据角色ID获取角色信息
        /// </summary>
        /// <param name="idKey">角色ID</param>
        /// <returns>包含角色信息的对象</returns>
        public RolePower GetRoleById(string idKey)
        {
            RolePower roleInfo = new RolePower();
            string sqlRoleInfo = "select * from role_power where id_key=" + idKey;
            DataRow drRoleInfo = oledb.GetDataRow(sqlRoleInfo);
            if (drRoleInfo != null)
            {
                roleInfo.IdKey = Convert.ToInt32(drRoleInfo["id_key"]);
                roleInfo.RoleName = drRoleInfo["role_name"].ToString();
                roleInfo.PowerCode = drRoleInfo["power_code"].ToString();
            }
            return roleInfo;
        }

        /// <summary>
        /// 根据角色ID号获取角色名
        /// </summary>
        /// <param name="idKey">角色ID号</param>
        /// <returns>角色名</returns>
        public string GetRoleNameById(string idKey)
        {
            string sqlGetName = "select  ROLE_NAME from role_power where id_key=" + idKey;
            string roleName = "";
            try
            {
                roleName = oledb.GetDataRow(sqlGetName)[0].ToString();
            }
            catch
            {
            }
            return roleName;
        }

        /// <summary>
        /// 转化权限码
        /// </summary>
        /// <param name="codes">待转化的权限码数组</param>
        /// <returns>转化后的权限码</returns>
        public string ConverToPowerCode(int[] codes)
        {
            Array.Sort(codes);
            string PowerCode = "";
            for (int i = 0; i < codes.Length; i++)
            {
                PowerCode += codes[i].ToString() + "|";
            }
            return PowerCode;
        }

        /// <summary>
        /// 排序权限码
        /// </summary>
        /// <param name="powerCode">待排序的权限码</param>
        /// <returns>排序好的权限码</returns>
        public string SortPowerCode(string powerCode)
        {
            string[] strCodes = powerCode.Split('|');
            Array.Sort(strCodes);
            string newPowerCode = "";
            for (int i = 0; i < strCodes.Length; i++)
            {
                newPowerCode += strCodes[i] + "|";
            }
            return newPowerCode;
        }
    }
}
